#!/bin/sh

article_parser="python3 parse_articles.py"
comment_parser="python3 parse_comments.py"
tubeFeeder="python3 ../common/tubeFeeder.py"
errorReporter="../common/report/errorReporter.sh"

master_lists="../common/master_lists"

last_run=0
interval=900

while true
do
    # get epoch time
    now=`date +%s`
    
    time_since_last_run=`expr $now - $last_run`

    # only run if it has been at least fifteen minutes since the previous run started
    if test $time_since_last_run -ge $interval #TODO make 3600
    then
        # get current date time to uniquely identify this run
        log_date=`date +%y-%m-%d-%H-%M`
        
        # prepare command to append stdout to current log file 
        log="tee -a logs/$log_date.log"

        # set last run time to now
        last_run=`date +%s`
        
        # print start time
        echo "start $now" | $log
        
        # parse article feed and place found media links into links.json
        $article_parser 2>&1 | $log
        # BUG TODO this appears to check `tee` return value, not the parser
        # print error and skip tubeFeeder.py if parsing failed
        return_value=$?
        if test $return_value -ne 0
        then
            echo "\033[31mparse_articles.py returned $return_value\033[0m" | $log
        else
            # print links.json
            cat links.json | $log
            # pull the master done list into the list subdirectory
            flock $master_lists/$list_name -c "cp $master_lists/doneList lists/masterList" 2>&1 | $log
            
            # get parsed links and upload to IA using TubeUp
            $tubeFeeder 2>&1 | $log
            
            # BUG TODO this appears to check `tee` return value, not the tube feeder
            # print error if tubeFeeder.py returned non-zero
            return_value=$?
            if test $return_value -ne 0
            then
                echo "\033[31mtubeFeeder.py returned $return_value\033[0m" | $log
            fi
            
            # remove duplicates from local lists then safely append local lists to master lists
            for list in lists/*
            do
                # isolate filename
                list_name=`echo $list | sed "s/.*\///"`
                
                # ignore the local copy of the master done list
                if test $list_name = masterList
                then
                    continue
                fi
                
                # sort and remove duplicates from this directory's list
                cat $list | sort | uniq > ${list}_temp
                mv ${list}_temp $list
                
                # safely append this directory's list to the corresponding master list and remove duplicates
		flock $master_lists/$list_name -c "cat $master_lists/$list_name $list | sort | uniq > $master_lists/${list_name}_temp; mv $master_lists/${list_name}_temp $master_lists/$list_name" 2>&1 | $log
            done
        fi
        
        # parse comments feed and place found media links into links.json
        $comment_parser 2>&1 | $log
        # BUG TODO this appears to check `tee` return value, not the parser
        # print error and skip tubeFeeder.py if parsing failed
        return_value=$?
        if test $return_value -ne 0
        then
            echo "\033[31mparse_comments.py returned $return_value\033[0m" | $log
        else
            # print links.json
            cat links.json | $log
            # pull the master done list into the list subdirectory
            flock $master_lists/$list_name -c "cp $master_lists/doneList lists/masterList" 2>&1 | $log
            
            # get parsed links and upload to IA using TubeUp
            $tubeFeeder 2>&1 | $log
            
            # BUG TODO this appears to check `tee` return value, not the tube feeder
            # print error if tubeFeeder.py returned non-zero
            return_value=$?
            if test $return_value -ne 0
            then
                echo "\033[31mtubeFeeder.py returned $return_value\033[0m" | $log
            fi
            
            # remove duplicates from local lists then safely append local lists to master lists
            for list in lists/*
            do
                # isolate filename
                list_name=`echo $list | sed "s/.*\///"`
                
                # ignore the local copy of the master done list
                if test $list_name = masterList
                then
                    continue
                fi
                
                # sort and remove duplicates from this directory's list
                cat $list | sort | uniq > ${list}_temp
                mv ${list}_temp $list
                
                # safely append this directory's list to the corresponding master list and remove duplicates
		flock $master_lists/$list_name -c "cat $master_lists/$list_name $list | sort | uniq > $master_lists/${list_name}_temp; mv $master_lists/${list_name}_temp $master_lists/$list_name" 2>&1 | $log
            done
        fi
        
        # safely process log to report errors
        flock $errorReporter -c "sh $errorReporter 'logs/$log_date.log'"
        
        # print finish time
        echo "finish `date +%s`" | $log 
    else
        # sleep until fifteen minutes has passed since the last run started
        sleep `expr $interval - $time_since_last_run`s
    fi
done
